<?php
namespace app\backend\controller;
use \think\Controller;
use think\Db;
use think\Request;

class Image extends Controller
{
  public function Upload(){
    $file = request()->file('image');
    // 移动到框架应用根目录/public/uploads/ 目录下
    if($file){
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            $filepath = $info->getSaveName();
            return $filepath;
        }else{
           return $file->getError();
        }
    }
  }
  public function Move(Request $request){
    $filepath = $request->param()['submitList'];
    $path_1 = ROOT_PATH . 'public' . DS . 'uploads/';
    $path_2 = ROOT_PATH . 'public' . DS . 'static/img/uploads/';
    $path_3 = "https://www.fxpixels.site/tp5/public/static/img/uploads/";
    $path_4 = [];
    for ($i = 0; $i < count($filepath); $i++){
      if(file_exists($path_1.$filepath[$i])){
        $date = explode('/',$filepath[$i]);
        $date = $date[0];
        if(file_exists($path_2.$date)){
          if(rename($path_1.$filepath[$i], $path_2.$filepath[$i])){
            array_push($path_4, $path_3.$filepath[$i]);
          }
        }else{  
          if(mkdir($path_2.$date)){
            if(rename($path_1.$filepath[$i], $path_2.$filepath[$i])){
              // return $filepath[$i];
              array_push($path_4, $path_3.$filepath[$i]);
            }
          }
        }
        
      }
    }
    return json_encode($path_4);
    
  }
}